package com.talkyun.openx.client;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Abstract proxy class
 * <p/>
 * Created by kevin on 2015/1/29.
 */
public abstract class AbstractProxy implements InvocationHandler {
    protected String mapping;
    protected String url;
    protected int timeout = (int) SECONDS.toMillis(3);

    protected String httpProxyAddress; // host:port
    protected String httpProxyAuthorization; // username:password (base64)

    public void setUrl(String url) {
        this.url = url;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public void setHttpProxyAddress(String httpProxyAddress) {
        this.httpProxyAddress = httpProxyAddress;
    }

    public void setHttpProxyAuthorization(String httpProxyAuthorization) {
        this.httpProxyAuthorization = httpProxyAuthorization;
    }

    @Override
    public final Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getDeclaringClass() == Object.class) {
            return this.toString() + "@" + "openx";
        }

        // really invoke to remote
        return doInvoke(proxy, method, args);
    }

    protected abstract Object doInvoke(Object proxy, Method method, Object[] args) throws Throwable;

}
